﻿using Guoc.BigMall.Infrastructure;
using Guoc.BigMall.Infrastructure.Extension;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Guoc.BigMall.Domain.Entity
{
    /// <summary>
    ///  系统内部会员用户
    /// </summary>
   public class Member:BaseEntity
    {

        public Member() {
            this.CreatedOn = DateTime.Now;
            this.Level = MemberLevel.Normal;
            this.UpdaptedOn = DateTime.Now;
            this.Status = MemberStatus.Active;
            this.Password = "123456";
            this.InitEncryptionPassword();
            this.PassList = new List<MemberPass>();
        }

         public string NickName { get; set; }

         public string RealName { get; set; }
        public string HeadImage { get; set; }

        public string Password { get; set; }

        /// <summary>
        ///  电话，唯一, 可以根据此号码登陆
        /// </summary>
        public string Phone { get; set; }
        /// <summary>
        ///  联系电话
        /// </summary>

        public string  SecondPhone { get; set; }

        public MemberStatus Status { get; set; }
        /// <summary>
        /// 支付宝账号
        /// </summary>
        public string AlipayId { get; set; }


        /// <summary>
        ///  会员等级
        /// </summary>
        public MemberLevel Level { get; set; }

        public DateTime CreatedOn { get; set; }

        public DateTime UpdaptedOn { get; set; }

        /// <summary>
        /// 会员拥有的 通信证
        /// </summary>
        public List<MemberPass> PassList { get; set; }

        /// <summary>
        /// 加密密码
        /// </summary>
        public void EncryptionPassword()
        {
            MD5 md5Prider = MD5.Create();
            this.Password = md5Prider.GetMd5Hash(this.Password);
        }
        /// <summary>
        /// 初始化密码时，前后台一致
        /// </summary>
        public void InitEncryptionPassword()
        {
            MD5 md5Prider = MD5.Create();
            this.Password = md5Prider.GetMd5Hash(this.Password);
            // this.WebPassword = this.Password;           
        }

        public void CheckPassword(string password)
        {
            MD5 md5Prider = MD5.Create();
            if (!md5Prider.VerifyMd5Hash(password, this.Password))
            {
                throw new FriendlyException("账户或密码不正确!");
            }

        }

        public void CheckAccountState()
        {
            if (this.Status != MemberStatus.Active) { throw new FriendlyException("账户未激活"); }
        }


    }

    public enum MemberStatus {
        [Description("激活")]
        /// <summary>
        /// 激活
        /// </summary>
        Active = 1,       
        [Description("禁用")]
        /// <summary>
        /// 禁用
        /// </summary>
        Disabled = 2,
        [Description("删除")]
        /// <summary>
        /// 删除
        /// </summary>
        Deleted = 3
    }

    public enum MemberLevel {
        [Description("普通")]
        Normal =1,
        [Description("会员")]
        Vip =2
    }
}
